package com.base.controller;

import com.base.po.voip.Adversiments;
import com.base.po.voip.SelfShopBrowse;
import com.base.po.voip.Users;
import com.base.service.AdversimentsService;
import com.base.service.SelfShopBrowseService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 自营商城浏览记录
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_AdvBrowse {
    @Autowired
    private UsersService usersService;
    @Autowired
    private SelfShopBrowseService selfShopBrowseService;
    @Autowired
    private AdversimentsService adversimentsService; // 轮播广告Service

    // 自营商城订单首页
    @RequestMapping("/advBrowse")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        // 浏览记录
        List<SelfShopBrowse> selfShopBrowse = null;
        //累计浏览
        int allBrowse = 0;
        page.setCurrentPageNo(1);
//        if (users.getRole() == 0) {
//            allBrowse = selfShopBrowseService.countByParentID(null, null);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, null, null);
//        }

        if (users.getRole() == 1) {
//            allBrowse = selfShopBrowseService.countByParentID(adminUserId, adminUserId);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, adminUserId, adminUserId);
            allBrowse = allBrowse + users.getBrowse();
        }

        if (users.getRole() == 2) {
//            allBrowse = selfShopBrowseService.countByParentID(users.getParentid(), adminUserId);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, users.getParentid(), adminUserId);
            Users pUser = usersService.selectByPrimaryKey(users.getParentid());
            allBrowse = allBrowse + pUser.getBrowse();
        }

//        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
//        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        List<Adversiments> adversiments = adversimentsService.selectByAll(new Adversiments(adminUserId, true));

        for (Adversiments adv : adversiments){
            switch (adv.getAdtype()){
                case 1:
                    request.setAttribute("adv1Path", adv.getPath());
                    request.setAttribute("adv1Time", adv.getCreatetime());
                    break;
                case 2:
                    request.setAttribute("adv2Path", adv.getPath());
                    request.setAttribute("adv2Time", adv.getCreatetime());
                    break;
                case 3:
                    request.setAttribute("adv3Path", adv.getPath());
                    request.setAttribute("adv3Time", adv.getCreatetime());
                    break;
                case 4:
                    request.setAttribute("adv4Path", adv.getPath());
                    request.setAttribute("adv4Time", adv.getCreatetime());
                    break;
                case 5:
                    request.setAttribute("adv5Path", adv.getPath());
                    request.setAttribute("adv5Time", adv.getCreatetime());
                    break;
                case 6:
                    request.setAttribute("adv6Path", adv.getPath());
                    request.setAttribute("adv6Time", adv.getCreatetime());
                    break;
                case 7:
                    request.setAttribute("adv7Path", adv.getPath());
                    request.setAttribute("adv7Time", adv.getCreatetime());
                    break;
                case 8:
                    request.setAttribute("adv8Path", adv.getPath());
                    request.setAttribute("adv8Time", adv.getCreatetime());
                    break;
            }
        }
        request.setAttribute("adv1Browse", 58013 * 8 + allBrowse);
        request.setAttribute("adv2Browse", 44013 * 7 + allBrowse);
        request.setAttribute("adv3Browse", 36123 * 8 + allBrowse);
        request.setAttribute("adv4Browse", 61513 * 10 + allBrowse);
        request.setAttribute("adv5Browse", 46013 + allBrowse);
        request.setAttribute("adv6Browse", 12921 + allBrowse);
        request.setAttribute("adv7Browse", 7486 * 8 + allBrowse );
        request.setAttribute("adv8Browse", 12921 + allBrowse);

        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_AdvBrowse");
        return modelAndView;
    }

//    @RequestMapping("/selectSelfBrowse")
//    public ModelAndView selectSelfBrowse(HttpServletRequest request, Integer curPage) {
//        // 设置session缓存
//        if (curPage == null) {
//            if (request.getSession().getAttribute("curPage") != null) {
//                curPage = (Integer) request.getSession().getAttribute("curPage");
//            } else {
//                curPage = 1;
//            }
//        }
//        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
//        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//
//        ModelAndView modelAndView = new ModelAndView();
//        Page page = new Page();
//        Users users = usersService.selectByPrimaryKey(adminUserId);
//
//        //已有商品
//        List<SelfShopBrowse> selfShopBrowse = null;
//        //累计浏览
//        int allBrowse = 0;
//        page.setCurrentPageNo(curPage);
//        if (users.getRole() == 0) {
//            allBrowse = selfShopBrowseService.countByParentID(null, null);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, null, null);
//        }
//
//        if (users.getRole() == 1) {
//            allBrowse = selfShopBrowseService.countByParentID(adminUserId, adminUserId);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, adminUserId, adminUserId);
//            allBrowse = allBrowse + users.getBrowse();
//        }
//
//        if (users.getRole() == 2) {
//            allBrowse = selfShopBrowseService.countByParentID(users.getParentid(), adminUserId);
//            page.setRecordTotal(allBrowse);
//            selfShopBrowse = selfShopBrowseService.selectByParentID(page, users.getParentid(), adminUserId);
//            Users pUser = usersService.selectByPrimaryKey(users.getParentid());
//            allBrowse = allBrowse + pUser.getBrowse();
//        }
//
//        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
//        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
//
//        request.setAttribute("allBrowse", allBrowse);
//        request.setAttribute("selfShopBrowse", selfShopBrowse);
//        request.setAttribute("adminUsergrade", adminUsergrade);
//        // 设置页面进行跳转
//        modelAndView.setViewName("jsp/index_SelfShopBrowse");
//        return modelAndView;
//    }
}
